package interview.stackwithmin;

public class StackWithMin {
	int capacity;
	int top;
	StackElement[] stackArray;
	
	
	public StackWithMin(int capacity) {
		this.capacity = capacity;
		stackArray = new StackElement[capacity];
		top = 0;
	}
	
	public int count() {
		return top;
	}
	
	public void push(int element) throws Exception {
		if(top == capacity) {
			throw new Exception("Stack full");
		}
		
		int min = top == 0? element : Math.min(element, stackArray[top-1].getMin());
		stackArray[top] = new StackElement(element, min);
		top++;
	}
	
	public int pop() throws Exception {
		if(top == 0)
			throw new Exception("Empty Stack");
		
		top--;
		return stackArray[top].getData();
	}
	
	public int min() throws Exception {
		if(top == 0)
			throw new Exception("Empty Stack");
		
		return stackArray[top - 1].getMin();
	}
}
